<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\AccountKeep as AccountKeepModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;
use app\common\model\Renzhensiyuan as RenzhensiyuanModel;
use app\common\model\Company as CompanyModel;
use app\common\model\ManagerPassword as ManagerPasswordModel;
use app\common\model\ManagerCompany as ManagerCompanyModel;
use app\common\model\Manager as ManagerModel;
use app\common\model\Member as MemberModel;
use app\common\model\ManagerJob as ManagerJobModel;
use app\common\model\ManagerDuty as ManagerDutyModel;
use app\common\model\Mzongpai as MzongpaiModel;
/**
 * 寺院认证控制器
 * Class Recharge
 * @package app\home\controller
 */
class Renzhengsiyuan extends Base
{
    /**
     * [index  我的寺院列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $this->getPageAndSize($param);
        $model = new ManagerModel;
        $cmodel = new CompanyModel;
        $mcmodel = new ManagerCompanymodel;
        $mem = $this->user;
        try{
            $manager = $model->findData($mem['tel'],'tel','id');
            $mcompany = $mcmodel->getAllData(['mid'=>$manager['id'],'status'=>1],'id','comid');
            foreach($mcompany as &$v){
                $company = $cmodel->findData($v['comid'],'id','id,comname,compic,states');
                $http = substr($company['compic'],0,4);
                if($http != 'http'){
                    $v['compic'] = config('domain_name').$company['compic'];
                }else{
                    $v['compic'] = $company['compic'];
                }
                $v['id'] = $company['id'];
                $v['comname'] = $company['comname'];
                $v['states'] = $company['states'];
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',$mcompany);
    }
    /**
     * [detial  我的寺院详情]
     * @return json
     * @author [C。]
     */
    public function detial()
    {
        $param = input('param.');
        $model = new RenzhensiyuanModel;
        $cmodel = new CompanyModel;
        $mpmodel = new ManagerPasswordModel;
        $mcmodel = new ManagerCompanyModel;
        $mmodel = new ManagerModel;
        $memodel = new MemberModel;
        $jmodel = new ManagerJobModel;
        $dmodel = new ManagerDutyModel;
        $mzmodel = new MzongpaiModel;
        $omodel = new CompanyGongdengOrderModel;
        $akmodel = new AccountKeepModel;
        $mem = $this->user;
        try{
            //$renzheng = $model->findData($param['id'],'id','compic,comname,city,addr,tel,zongpai,duty,activepic,jiedie,cid');
            $my_manager = $mmodel->findData($mem['tel'],'tel','id');
            if(empty($param['id'])){
                return admin_json(-1, '获取失败');
            }
            $company = $cmodel->findData($param['id'],'id','compic,comname,city,addr,phone');
            $http = substr($company['compic'],0,4);
            if($http != 'http'){
                $company['compic'] = config('domain_name').$company['compic'];
            }

            $managercompany = $mcmodel->findOne(['comid'=>$param['id'],'mid'=>$my_manager['id']],'dutyid,jobid');

//            $zmanager = $mmodel->findData($company['manager_id'],'id','jiedie');
//            $company['jiedie'] = $zmanager['jiedie'];
            if(!empty($managercompany)){
                if($managercompany['dutyid'] == 0){
                    $managercompany['dutyid'] = 2;
                }
            }else{
                return admin_json(-1, '还未加入寺院！');
            }

            $duty = $dmodel->findData($managercompany['dutyid'],'id','duty,rules');
            $company['duty'] = $duty['duty'];
            $company['rules'] = $duty['rules'];
            if(!empty($managercompany['jobid'])){
                $mjob = $jmodel->findData($managercompany['jobid'],'id','job');
                $company['job'] = $mjob['job'];
            }else{
                $company['job'] = '无';
            }

            $company['guanwang'] = 'https://wx.fushan8.com/#/temple_info?id='.$param['id'];
            $company['xitong'] = 'http://www.zhsaas.com?id='.$param['id'];
            $managerpass = $mpmodel->findOne(['comid'=>$param['id'],'mid'=>$my_manager['id']],'tel,pass');

            $company['zhanghao'] = $managerpass['tel'];
            $company['pass'] = $managerpass['pass'];
            $managercom = $mcmodel->getlist(['comid'=>$param['id'],'status'=>1],0,5,'mid,jobid');
            $mem = array();
            foreach ($managercom as $k=>$v){
                $manager = $mmodel->findData($v['mid'],'id','tel');
                $member = $memodel->findData($manager['tel'],'tel','tel,username,img');
                if(!empty($v['jobid'])){
                    $job = $jmodel->findData($v['jobid'],'id','job');
                    $member['job'] = $job['job'];
                }else{
                    $member['job'] = '无';
                }
                $http = substr($member['img'],0,4);
                if($http != 'http'){
                    $member['img'] = config('domain_name').$member['img'];
                }
                $member['mid'] = $v['mid'];
                $mem[$k] = $member;
            }

            $company['member'] = $mem;
            $all_get_order_price = $omodel->getSum(['status'=>1,'comid'=>$param['id']],'total_price');
            $all_get_account = $akmodel->getSum(['comid'=>$param['id'],'type'=>1],'price');
            $all_out_account = $akmodel->getSum(['comid'=>$param['id'],'type'=>2],'price');
            $company['all_get_account'] = $all_get_order_price + $all_get_account;
            $company['all_out_account'] = $all_out_account;
            $company['all_residue_account'] = $company['all_get_account'] - $all_out_account;
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$company);
    }
    /**
     * [more  获取更多人事]
     * @return json
     * @author [C。]
     */
    public function more()
    {
        $param = input('param.');
        $mcmodel = new ManagerCompanyModel;
        $mmodel = new ManagerModel;
        $memodel = new MemberModel;
        $jmodel = new ManagerJobModel;
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['comid'] = $param['cid'];
            $map['status'] = 1;
            $count = $mcmodel->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $mcmodel->getlist($map, $this->from, $this->size,'mid,jobid,dutyid');
            $mem = array();
            foreach ($lists as $k=>$v){
                $manager = $mmodel->findData($v['mid'],'id','id,tel');
                $member = $memodel->findData($manager['tel'],'tel','tel,username,img,sex,bathday');
                $http = substr($member['img'],0,4);
                if($http != 'http'){
                    $member['img'] = config('domain_name').$member['img'];
                }
                if(!empty($v['jobid'])){
                    $job = $jmodel->findData($v['jobid'],'id','job');
                    $member['job'] = $job['job'];
                }
                if($v['dutyid'] == 0){
                    $v['dutyid'] = 2;
                }
                $member['jobid'] = $v['jobid'];
                $member['dutyid'] = $v['dutyid'];
                $member['mid'] = $v['mid'];
                $mem[$k] = $member;
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $mem,  //列表数据
        ]);
    }
}